package com.controller;

import com.po.Comment;
import com.po.User;
import com.service.CommentService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by 阳幸子 on 2018/5/6.
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;
    @RequestMapping("/user_index")
    public String findUserIndexHtml(){
        return "TFSystem/user_index";
    }
    @RequestMapping(value = "/user_register",method = RequestMethod.POST)
    public String register(@ModelAttribute User user){
        System.out.println(user);
        userService.register(user);
      return "login";
    }

    @RequestMapping(value = "/user_manager")
    public String user_manager(int page, Model model){
        int count=userService.count();
        int pageSize=0;
        if(count%6==0){
            pageSize=count/6;
        }else{
            pageSize=count/6+1;
        }
        if(page>0){
            page=(page-1)*6;
        }
        List<User> listuser=userService.selectAllUser();
        model.addAttribute("listUser",listuser);
        model.addAttribute("countUser",count);
        model.addAttribute("pageSize",pageSize);
        return "/manager/user_manager";
    }
    @RequestMapping("/selectUserById")
    public String  showUser(long id,Model model){
        User user=userService.selectById(id);
        model.addAttribute("user",user);
        return "/manager/user_detail";
    }
    @RequestMapping("/edit_user")
    public String edit_user(long id,Model model){
        User user=userService.selectById(id);
        model.addAttribute("user",user);
        return "/manager/edit_user";
    }
    @RequestMapping(value="/edit_userForm",method=RequestMethod.POST)
    public String edit_userForm(long id,User user){
        user.setId(id);
        userService.updateUser(user);
        return "redirect:/user_manager?page=1";
    }
    @RequestMapping("/add_user")
    public String add_user(){
        return "/manager/add_user";
    }
    @RequestMapping(value = "/add_userForm",method = RequestMethod.POST)
    public String  add_userForm(User user){
        user.setUsername(user.getVipnum());
        user.setPassword("111111");
        userService.register(user);
        return "redirect:/user_manager?page=1";
    }


    @RequestMapping("/del_user")
    public String  del_user(long id){
        userService.del_user(id);
        return "redirect:/user_manager?page=1";
    }
    @RequestMapping("/commentForm")
    public String comment(long id,long course_id,String content ){
        Comment comment=new Comment();
        comment.setUserId(id);
        comment.setCourseId(course_id);
        comment.setContent(content);
        commentService.insertComment(comment);
        return "redirect:/selectCourseDetail?id="+id;
    }


    @RequestMapping("/user_plain")
    public String user_plain(){
        return "/user/user_plain";
    }

}
